package com.fortune.service.impl;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fortune.service.SmsService;
/**
 * 
 * @author ran
 *
 */
@Service
public class SmsServiceImpl implements SmsService{
	private Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);
	private String url = "http://utf8.api.smschinese.cn";
	private String uid = "AllLotus_Haolian_VIP";
	private String key = "HebeiHandanGuantaoLvzhuang057750";
	
	public static void main(String[] args) throws Exception {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://utf8.api.smschinese.cn");
		post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
		NameValuePair[] data = { 
				new NameValuePair("Uid", "AllLotus_Haolian_VIP"), 
				new NameValuePair("Key", "HebeiHandanGuantaoLvzhuang057750"),
				new NameValuePair("smsMob", ""), 
				new NameValuePair("smsText", "") };
		post.setRequestBody(data);
		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
		System.out.println(result); // 打印返回消息状态
		post.releaseConnection();
	}

	@Override
	public boolean sendResetCode(String phone, String code) {
		try {
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(url);
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
			NameValuePair[] data = { 
					new NameValuePair("Uid", uid), 
					new NameValuePair("Key", key),
					new NameValuePair("smsMob", phone), 
					new NameValuePair("smsText", "重置密码验证码：" + code) };
			post.setRequestBody(data);
			client.executeMethod(post);
			int statusCode = post.getStatusCode();
			String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
			post.releaseConnection();
			if (statusCode!=200) {
				return false;
			}
			if (Integer.valueOf(result)<1) {
				return false;
			}
		} catch (Exception e) {
			logger.error("短信发送失败",e);
		}
		
		return true;
	}

	@Override
	public boolean sendRegisteredCode(String phone, String code) {
		try {
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(url);
			post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
			NameValuePair[] data = {
					new NameValuePair("Uid", uid),
					new NameValuePair("Key", key),
					new NameValuePair("smsMob", phone),
					new NameValuePair("smsText", "您正在使用晟财宝APP注册账号，您的验证码为：" + code + "请勿告诉他人") };
			post.setRequestBody(data);
			client.executeMethod(post);
			int statusCode = post.getStatusCode();
			String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
			post.releaseConnection();
			if (statusCode!=200) {
				return false;
			}
			if (Integer.valueOf(result)<1) {
				return false;
			}
		} catch (Exception e) {
			logger.error("短信发送失败",e);
		}

		return true;
	}
}
